//
//  UIViewController+HDRepairListController.m
//  app
//
//  Created by D on 15/8/31.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDRepairListController.h"
#import "HDRepairListResult.h"
#import "HDRepairManager.h"
#import "HDRepairListModle.h"
#import "HDRepairListCell.h"
#import "HDRepairCodeView.h"
#import "HDRepairDetailController.h"
#import "HDJiamubiaoView.h"

static const CGFloat kCartTotalPanelHeight = 120;

@interface HDRepairListController ()<UITableViewDataSource,UITableViewDelegate,HDRepairListCellDelegate>

@property (nonatomic,strong) HDRepairManager * repairManager;
@property (nonatomic,strong) HDRepairListResult * repairResult;
@property (nonatomic,strong) UITableView * repairTableView;
@property (nonatomic,strong) UILabel * centerLab;
@property (nonatomic,assign) int page;
@property (nonatomic,strong) HDRepairListJiamubiaoResult * jiamubiao_result;

@end

@implementation HDRepairListController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"维修列表";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.repairTableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 64),
                                                                         [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-kCartTotalPanelHeight ]) style:UITableViewStyleGrouped];
    
    self.repairTableView.headerRefreshController = [[UIRefreshHeaderController alloc] initWithTableView:self.repairTableView caller:[UBTargetCaller of:self action:@selector(refreshData)]];
    self.repairTableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc]initWithView:self.repairTableView viewController:self tryAgain:@selector(refreshData)];
    self.repairTableView.footerRefreshController = [[UIRefreshFooterController alloc]initWithTableView:self.repairTableView caller:[UBTargetCaller of:self action:@selector(loadMoreData)]];
    self.repairTableView.backgroundColor = [UIColor whiteColor];
    self.repairTableView.delegate = self;
    self.repairTableView.dataSource = self;
    
    self.repairTableView.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0];
    
    [self.view addSubview:self.repairTableView];
    
    self.repairManager = [HDRepairManager shareManager];
    self.repairResult = [[HDRepairListResult alloc]initWithTarget:self action:@selector(repairListResult:)];
    self.jiamubiao_result = [[HDRepairListJiamubiaoResult alloc]initWithTarget:self action:@selector(jiamubiaoResult:)];
    
    [self.repairTableView.nothingRefreshView show];
    [self refreshData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - 发送请求
-(void)resultRepairList
{
    NSDictionary * params = @{@"mall_com_id":self.mall_com_id,
                              @"add_code":self.fullAddress,
                              @"page":[NSString stringWithFormat:@"%d",self.page]};
    
    [self.repairManager loadRepairListData:self.repairResult Params:params page:self.page];
}

#pragma mark - 刷新
- (void)refreshData
{
    self.page = 1;
    [self resultRepairList];
}
#pragma mark - 加载更多

-(void)loadMoreData
{
    self.page++;
    [self resultRepairList];
}


-(void)repairListResult:(HDRepairListResult*)rt
{
    [self.repairTableView endRefreshingAll];

    if (rt.success) {
        [self.repairTableView.nothingRefreshView remove];
        
        [self.repairTableView reloadData];
        if (rt.noData) {
            
            if (self.repairTableView.footerRefreshController) {
                [self.repairTableView clearFooter];
            }
            if (!self.centerLab) {
                CGFloat width = UBO_WINDOW.width - 24;
                self.centerLab = [[UILabel alloc]initWithFrame:CGRectOf([self.view.ubo fx:UB_1_2 ox:-width/2 fy:UB_1_2 oy:-16], [self.view.ubo ow:width oh:32])];
                self.centerLab.text = @"暂时没有报修内容";
                [self.centerLab setTextAlignment:NSTextAlignmentCenter];
                [self.centerLab setFont:[UIFont systemFontOfSize:15]];
                [self.view addSubview:self.centerLab];
            }
                        
        } else {
            if (!self.repairTableView.footerRefreshController) {
                self.repairTableView.footerRefreshController = [[UIRefreshFooterController alloc] initWithTableView:self.repairTableView caller:[UBTargetCaller of:self action:@selector(loadMoreData)]];
            }
        
            if (rt.noMoreData) {
                [MyToast showDefaultBottonWithText:@"无更多数据..."];
            }
            
        }
    } else {
        [self.repairTableView.nothingRefreshView activeStop];
        [MyToast showDefaultBottonWithText:rt.message];
    }

}

#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.repairResult.dataSourceArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    HDRepairListCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HDRepairListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.backgroundView = nil;
    }
    cell.delegate = self;
    HDRepairListModle * model = self.repairResult.dataSourceArray[[indexPath section]];
    
    [cell cellWithModel:model Row:indexPath.row];

    
   
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HDRepairListModle * model = self.repairResult.dataSourceArray[[indexPath section]];
    HDRepairDetailController * detailVC = [[HDRepairDetailController alloc]init];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 42;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HDRepairListModle * model = self.repairResult.dataSourceArray[section];
    HDRepairCodeView * codeView = [[HDRepairCodeView alloc]initWithModel:model];
    return codeView;
}


-(void)showJiamubiao:(UIButton *)sender
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * params = @{@"com_id":[NSString stringWithFormat:@"%ld",(long)sender.tag]};
    
    [self.repairManager loadJiamubiaoData:self.jiamubiao_result Params:params];
    
    
}

-(void)jiamubiaoResult:(HDRepairListJiamubiaoResult*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (rt.success) {
        
        if (rt.dataSourceArray.count > 0) {
            HDJiamubiaoView * view = [[HDJiamubiaoView alloc]initWithParams:rt.dataSourceArray];
            [view showJiamubiaoViewSheet];
        }else
        {
            [MyToast showWithText:@"暂无数据"];
        }
        
        
        
        
    }else
    {
        [MyToast showWithText:@"暂无数据"];
    }
    
    
    
}

@end












